<?php

namespace App\Services;

use App\Models\Contract;
use App\Models\Medical;

/**
 * 用户服务
 *
 * @Author hechengwei
 * @email chichiooo@sina.com
 * @DateTime 2024-01-25
 */
class MemberService
{

    // 根据用户id获取上传病例数量 所有病例 审核通过 审核未通过
    public static function getUserMedicalByUserId($userid, $status = '')
    {
        $where = function ($query) use ($status) {

            if ($status != '') {
                $query->where('status', $status);
            }
        };

        $medical = Medical::where('user_id', $userid)->where($where)->count();
        return $medical;
    }
    // 根据用户id获取协议数量  所有协议  审核通过  审核未通过
    public static function getUserContractByUserId($userid, $status = '')
    {
        $where = function ($query) use ($status) {

            if ($status != '') {
                $query->where('audit_status', $status);
            }
        };

        $contract = Contract::where('user_id', $userid)->where($where)->count();
        return $contract;
    }
    // 根据用户id获取已支付劳务合同数量
    public static function getUserContractPaymentByUserId($userid)
    {
        $where = function ($query) {
            $query->where('audit_status', 1);
            $query->where('is_pay', 1);
        };

        $contract = Contract::where('user_id', $userid)->where($where)->count();
        return $contract;
    }


    // 根据区域获取上传病例数量 所有病例 审核通过 审核未通过
    public static function getMedicalByRegion($region, $status = '')
    {
        $where = function ($query) use ($status) {
            if ($status != '') {
                $query->where('status', $status);
            }
        };

        $medical = Medical::where('region', $region)->where($where)->count();
        return $medical;
    }


    /**
     * 根据区域获取协议数量与支付数量
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-01-25
     * @param [type] $region 区域
     * @param string $status  审核状态
     * @param string $payStatus  支付状态
     * @return void
     */
    public static function getContractByRegion($region, $status = '', $payStatus = '')
    {
        $medical = Medical::withCount(['contract' => function ($query) use ($status, $payStatus) {
            if ($status != '') {
                $query->where('audit_status', $status);
            }
            if ($payStatus != '') {
                $query->where('is_pay', $payStatus);
            }
        }])->where('region', $region)->get();
        return $medical->pluck('contract_count')->sum();
    }
}
